package com.micro.ai.files.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.files.entity.FileFolder;
import com.micro.ai.files.service.FileFolderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 文件夹控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/file-folders")
@Tag(name = "文件夹管理", description = "文件夹的增删改查、移动、重命名等功能")
public class FileFolderController {

    @Autowired
    private FileFolderService fileFolderService;

    @PostMapping
    @PreAuthorize("hasAuthority('folder:create')")
    @Operation(summary = "创建文件夹", description = "创建新的文件夹")
    public ApiResponse<FileFolder> create(@Valid @RequestBody FileFolder folder) {
        FileFolder result = fileFolderService.create(folder);
        return ApiResponse.success("创建成功", result);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('folder:update')")
    @Operation(summary = "更新文件夹", description = "更新文件夹信息")
    public ApiResponse<FileFolder> update(
            @Parameter(description = "文件夹ID", required = true) @PathVariable String id,
            @Valid @RequestBody FileFolder folder) {
        FileFolder result = fileFolderService.update(id, folder);
        return ApiResponse.success("更新成功", result);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('folder:delete')")
    @Operation(summary = "删除文件夹", description = "删除文件夹")
    public ApiResponse<Void> delete(
            @Parameter(description = "文件夹ID", required = true) @PathVariable String id) {
        fileFolderService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    @PutMapping("/{id}/move")
    @PreAuthorize("hasAuthority('folder:update')")
    @Operation(summary = "移动文件夹", description = "将文件夹移动到新的父目录")
    public ApiResponse<FileFolder> move(
            @Parameter(description = "文件夹ID", required = true) @PathVariable String id,
            @Parameter(description = "新父文件夹ID") @RequestParam(value = "parentId", required = false) String parentId) {
        FileFolder result = fileFolderService.move(id, parentId);
        return ApiResponse.success("移动成功", result);
    }

    @PutMapping("/{id}/rename")
    @PreAuthorize("hasAuthority('folder:update')")
    @Operation(summary = "重命名文件夹", description = "重命名文件夹")
    public ApiResponse<FileFolder> rename(
            @Parameter(description = "文件夹ID", required = true) @PathVariable String id,
            @Parameter(description = "新文件夹名称", required = true) @RequestParam("name") String name) {
        FileFolder result = fileFolderService.rename(id, name);
        return ApiResponse.success("重命名成功", result);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('folder:view')")
    @Operation(summary = "获取文件夹详情", description = "根据ID获取文件夹详细信息")
    public ApiResponse<FileFolder> getById(
            @Parameter(description = "文件夹ID", required = true) @PathVariable String id) {
        FileFolder folder = fileFolderService.getById(id);
        if (folder == null) {
            return ApiResponse.error("F0002", "文件夹不存在");
        }
        return ApiResponse.success(folder);
    }

    @GetMapping("/page")
    @PreAuthorize("hasAuthority('folder:view')")
    @Operation(summary = "分页查询文件夹", description = "支持多条件查询文件夹列表")
    public ApiResponse<Page<FileFolder>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(value = "tenantId", required = false) String tenantId,
            @Parameter(description = "父文件夹ID") @RequestParam(value = "parentId", required = false) String parentId,
            @Parameter(description = "关键词") @RequestParam(value = "keyword", required = false) String keyword) {
        Page<FileFolder> page = fileFolderService.pageQuery(pageNum, pageSize, tenantId, parentId, keyword);
        return ApiResponse.success(page);
    }

    @GetMapping("/children")
    @PreAuthorize("hasAuthority('folder:view')")
    @Operation(summary = "查询子文件夹", description = "查询指定文件夹的子文件夹列表")
    public ApiResponse<List<FileFolder>> listChildren(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "父文件夹ID") @RequestParam(value = "parentId", required = false) String parentId) {
        List<FileFolder> list = fileFolderService.listChildren(tenantId, parentId);
        return ApiResponse.success(list);
    }

    @GetMapping("/tree")
    @PreAuthorize("hasAuthority('folder:view')")
    @Operation(summary = "查询文件夹树", description = "查询文件夹树形结构")
    public ApiResponse<List<FileFolder>> listTree(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "父文件夹ID") @RequestParam(value = "parentId", required = false) String parentId) {
        List<FileFolder> tree = fileFolderService.listTree(tenantId, parentId);
        return ApiResponse.success(tree);
    }
}

